﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Workbench.Annotations;
using SharedGenomics.Workbench.Views;
using SharedGenomics.Workbench.PresentationModel;

namespace SharedGenomics.Workbench.Presenters
{
    public class AnnotationOptionsPresenter : Presenter
    {
        private readonly IAnnotationOptionsView _optionsView;
        private readonly AnnotationManager _annotationManger;

        public AnnotationOptionsPresenter(IAnnotationOptionsView view, AnnotationManager annotationManager)
            : base(view)
        {
            view.ViewLoaded += View_ViewLoaded;
            view.SaveAndCloseClicked += View_SaveAndCloseClicked;
            view.CancelClicked += (o, e) => { this.RequestClose(); };

            this._optionsView = view;
            this._annotationManger = annotationManager;
        }

        private void View_ViewLoaded(object sender, EventArgs e)
        {
            //show current annotation activation statuses
            this._optionsView.ActivationStatuses = this._annotationManger.AnnotationActivationStatuses.Select(kvp => new AnnotationActivationStatus
            {
                AnnotationType = kvp.Key.AnnotationType,
                Description = kvp.Key.Description,
                Name = kvp.Key.Name,
                IsActive = kvp.Value
            });
        }

        private void View_SaveAndCloseClicked(object sender, EventArgs e)
        {
            //get the activation statuses from the view and update the annotation manager
            foreach (var annotationActivation in this._optionsView.ActivationStatuses)
            {
                this._annotationManger.SetAnnotationActive(annotationActivation.AnnotationType, annotationActivation.IsActive);
            }

            this._annotationManger.Save();
            this.RequestClose();
        }
    }
}
